package com.wejoy.video.middle.station.data.service.alarm.impl;

import com.wejoy.video.middle.station.data.domain.workorder.WorkOrderInfo;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.alarm.IAlarmDetailsMapper;
import com.wejoy.video.middle.station.data.domain.alarm.AlarmDetails;
import com.wejoy.video.middle.station.data.service.alarm.IAlarmDetailsService;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.AlarmDataVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 工单主表
 *
 * @author miracle_j
 * @date 2024年12月12日 15:18:52
 */
@Service
public class AlarmDetailsServiceImpl implements IAlarmDetailsService {
    @Autowired
    private IAlarmDetailsMapper alarmDetailsMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(AlarmDetails req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(AlarmDetails req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return alarmDetailsMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(AlarmDetails alarmDetails) {
        WJAssert.notNull(alarmDetails, "入参对象不能为空");
        WJAssert.notNull(alarmDetails.getId(), "id参数错误");
        AlarmDetails where = new AlarmDetails();
        where.setId(alarmDetails.getId());
        return alarmDetailsMapper.updateSelective(alarmDetails, where);
    }

    @Override
    public int getCount(AlarmDetails alarmDetails) {
        checkStringLength(alarmDetails, true);
        return alarmDetailsMapper.getCount(alarmDetails);
    }

    @Override
    public AlarmDetails getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        AlarmDetails where = new AlarmDetails();
        where.setId(id);
        return alarmDetailsMapper.getOne(where);
    }

    @Override
    public List<AlarmDetails> getList(AlarmDetails alarmDetails) {
        checkStringLength(alarmDetails, true);
        return alarmDetailsMapper.getList(alarmDetails);
    }

    @Override
    public List<AlarmDetails> getPageList(AlarmDetails alarmDetails) {
        checkStringLength(alarmDetails, true);
        return alarmDetailsMapper.getPageList(alarmDetails);
    }

    @Override
    public void audit(Long id, String operator) {
        AlarmDetails req = new AlarmDetails();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        AlarmDetails req = new AlarmDetails();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        AlarmDetails req = new AlarmDetails();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Override
    public List<Map<String,String>> getCountByalarmLocation(AlarmDetails alarmDetails) {
        return alarmDetailsMapper.getCountByalarmLocation(alarmDetails);
    }

    @Override
    public AlarmDetails getOneByEventId(String eventId) {
        WJAssert.notNull(eventId, "id参数错误");
        AlarmDetails where = new AlarmDetails();
        where.setEventId(eventId);
        return alarmDetailsMapper.getOne(where);
    }

//    @Override
//    public AlarmDataVO getAlarmDataByDate(AlarmDetails alarmDetails) {
//        return alarmDetailsMapper.getAlarmDataByDate(alarmDetails);
//    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(AlarmDetails alarmDetails, boolean nullAble) {
        WJAssert.notNull(alarmDetails, "入参对象不能为空");
        WJAssert.limitMaxLength(alarmDetails.getImageUrl(), true, 65535, "报警图像地址不合法!");
        WJAssert.limitMaxLength(alarmDetails.getBusinessScenario(), nullAble, 50, "报警类型对应枚举类不合法!");
        WJAssert.limitMaxLength(alarmDetails.getAlarmLocation(), true, 50, "报警类型不合法!");
        WJAssert.limitMaxLength(alarmDetails.getAffiliation(), true, 255, "所属机构不合法!");
        WJAssert.limitMaxLength(alarmDetails.getDeptName(), nullAble, 50, "部门名称不合法!");
        WJAssert.limitMaxLength(alarmDetails.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(alarmDetails.getRemark(), true, 255, "备注不合法!");
        WJAssert.limitMaxLength(alarmDetails.getEventId(), true, 80, "EVS事件id不合法!");
        WJAssert.limitMaxLength(alarmDetails.getImageAttributeJson(), true, 65535, "工单图像属性不合法!");
    }
}
